package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.constant.RedisMessageConstant;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    // 体检预约
    @RequestMapping("/check")
    public Result check(@RequestBody Map<String, String> map, HttpServletResponse response) {
        // 获取手机号和验证码（页面）
        String validateCode = map.get("validateCode");
        String telephone = map.get("telephone");
        // 通过手机号从Redis中获取验证码
        String code4 = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        //对手机号输入验证码做校验，页面输入的验证码和Redis中存放的验证码是否一致
        if (code4 == null || !code4.equals(validateCode)) {
            // 如果不一致，提示【验证码输入有误】
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        Result result = null;
        try {
            // 如果校验成功
            // 判断当前用户是否是会员，不是会员，自动注册会员
            Member member = memberService.findMemberByTelephone(telephone);
            if (member == null) {
                member = new Member();
                member.setPhoneNumber(telephone);
                member.setRegTime(new Date());// 注册时间当前时间
                memberService.add(member);
            }
            // 保存用户的登录状态,使用cookie
            Cookie cookie = new Cookie("login_member_telephone",telephone);
            cookie.setPath("/");// 有效路径
            cookie.setMaxAge(30*24*60*60);// 有效时间（单位：秒）
            response.addCookie(cookie);
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.LOGIN_FAIL);
        }
    }
}
